Slightly improve performance in engines/utils.py#8747
Slightly improve performance in engines/utils.py#8747benediktjohannes wants to merge 1 commit intoProject-MONAI:devfrom
Conversation
Signed-off-by: Benedikt Johannes <benedikt.johannes.hofer@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 📝 WalkthroughWalkthroughRefactored container initializations and dictionary operations in Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes 🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…r.py (#8749) ### Description See also #8747 and #8748 ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Benedikt Johannes <benedikt.johannes.hofer@gmail.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
| image, label = default_prepare_batch(batchdata, device, non_blocking, **kwargs) | ||
| args_ = list() | ||
| kwargs_ = dict() | ||
| args_ = [] |
There was a problem hiding this comment.
Here this might make a slight improvement since this method is called a lot, but I would suggest the following would be more efficient still (You'd have to test this):
def __call__(
self,
batchdata: dict[str, torch.Tensor],
device: str | torch.device | None = None,
non_blocking: bool = False,
**kwargs: Any,
) -> tuple[torch.Tensor, torch.Tensor, tuple, dict]:
"""
Args `batchdata`, `device`, `non_blocking` refer to the ignite API:
https://pytorch.org/ignite/v0.4.8/generated/ignite.engine.create_supervised_trainer.html.
`kwargs` supports other args for `Tensor.to()` API.
"""
image, label = default_prepare_batch(batchdata, device, non_blocking, **kwargs)
args_ = ()
kwargs_ = {}
def _get_data(key: str) -> torch.Tensor:
data = batchdata[key]
if isinstance(data, torch.Tensor):
data = data.to(device=device, non_blocking=non_blocking, **kwargs)
return data
if isinstance(self.extra_keys, (str, list, tuple)):
args_ = tuple(_get_data(k) for k in ensure_tuple(self.extra_keys))
elif isinstance(self.extra_keys, dict):
kwargs_ = {k: _get_data(v) for k, v in self.extra_keys.items()}
return cast(torch.Tensor, image), cast(torch.Tensor, label), args_, kwargs_
Description
This change probably improves performance slightly (unforetunately, I don't have a local instance of MONAI installed in order to test this, but I'm pretty sure from looking at the code that using [], {} and kwargs_[k] = get_data(v) should be semantically the same and probably slightly better in performance, see also #1423 (credits to @de-sh for the nice fix 👍 ) where at least [] and {} were changed as a refactoring and kwargs[k] = _get_data(v) just seems better to me becuase I think that this saves some time due to dictionary lookups and temporary objects, but I'm not quite sure because it wasn't tested, so please correct me if I'm mistaken).
A few sentences describing the changes proposed in this pull request.
Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.